package com.winexpress.session.product;

import com.winexpress.entity.Category;
import com.winexpress.entity.Wine;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class SearchSessionBean {

    @PersistenceContext
    private EntityManager em;

    public List<Category> searchCategory(String input) {
        List<Category> category = new ArrayList<>();

        String strQuery = "SELECT c FROM Category c";
        Query query = em.createQuery(strQuery);
        List<Category> temp = (List<Category>) query.getResultList();

        for (Object o : temp) {
            Category c = (Category) o;
            if (c.getName().toUpperCase().contains(input.toUpperCase())) {
                category.add(c);
            }
        }
        return category;
    }

    public List<Wine> searchByCategory(String input) {
        List<Wine> wine = new ArrayList<>();

        String strQuery = "SELECT c FROM Category c";
        Query query = em.createQuery(strQuery);
        List<Category> category = (List<Category>) query.getResultList();

        for (Object o : category) {
            Category c = (Category) o;
            if (c.getName().toUpperCase().contains(input.toUpperCase())) {
                for (Object obj : c.getWine()) {
                    Wine w = (Wine) obj;
                    if (w.isIsActive()) {
                        wine.add(w);
                    }
                }
            }
        }
        return wine;
    }

    public List<Wine> searchByWineName(String input) {
        List<Wine> wine = new ArrayList<>();

        String strQuery = "SELECT w FROM Wine w";
        Query query = em.createQuery(strQuery);
        List<Wine> temp = (List<Wine>) query.getResultList();

        for (Object o : temp) {
            Wine w = (Wine) o;
            if (w.getName().toUpperCase().contains(input.toUpperCase())) {
                wine.add(w);
            }
        }
        return wine;
    }

    public List<Wine> searchByCountry(String input) {
        List<Wine> wine = new ArrayList<>();

        String strQuery = "SELECT w FROM Wine w";
        Query query = em.createQuery(strQuery);
        List<Wine> temp = (List<Wine>) query.getResultList();

        for (Object o : temp) {
            Wine w = (Wine) o;
            if (w.getCountry().toUpperCase().contains(input.toUpperCase())) {
                wine.add(w);
            }
        }
        return wine;
    }

    public List<Wine> searchByVarietal(String input) {
        List<Wine> wine = new ArrayList<>();

        String strQuery = "SELECT w FROM Wine w";
        Query query = em.createQuery(strQuery);
        List<Wine> temp = (List<Wine>) query.getResultList();

        for (Object o : temp) {
            Wine w = (Wine) o;
            if (w.getVarietal().toUpperCase().contains(input.toUpperCase())) {
                wine.add(w);
            }
        }
        return wine;
    }

    public List<Wine> searchByVintage(String input) {
        String strQuery = "SELECT w FROM Wine w WHERE w.vintage = :vintage";
        Query query = em.createQuery(strQuery);
        query.setParameter("vintage", input);
        List<Wine> wine = (List<Wine>) query.getResultList();

        return wine;
    }

    public List<Wine> searchByRegion(String input) {
        List<Wine> wine = new ArrayList<>();

        String strQuery = "SELECT w FROM Wine w";
        Query query = em.createQuery(strQuery);
        List<Wine> temp = (List<Wine>) query.getResultList();

        for (Object o : temp) {
            Wine w = (Wine) o;
            if (w.getRegion().toUpperCase().contains(input.toUpperCase())) {
                wine.add(w);
            }
        }
        return wine;
    }

    public List<Wine> searchByPrice(String input) {
        List<Wine> wine = new ArrayList<>();

        String strQuery = "SELECT w FROM Wine w";
        Query query = em.createQuery(strQuery);
        query.setParameter("active", true);
        List<Wine> temp = (List<Wine>) query.getResultList();

        String[] st = input.split("-");
        Double upperPrice = null;
        if (!st[1].trim().isEmpty()) {
            upperPrice = new Double(st[1].substring(1).trim()).doubleValue();
        }
        Double lowerPrice = new Double(st[0].substring(1).trim()).doubleValue();

        if (upperPrice == null) {
            for (Object o : temp) {
                Wine w = (Wine) o;
                Double p = w.getPrice();
                if (p >= lowerPrice) {
                    System.out.println("wind id = " + w.getId());
                    wine.add(w);
                }
            }
        } else {
            for (Object o : temp) {
                Wine w = (Wine) o;
                Double p = w.getPrice();
                if (p <= upperPrice && p >= lowerPrice) {
                    System.out.println("wind id = " + w.getId());
                    wine.add(w);
                }
            }
        }
        return wine;
    }
}
